from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import SearchFilter
from rest_framework.generics import ListAPIView, ListCreateAPIView,RetrieveAPIView

from apps.meiduo_admin.serializers.userserialzier import UserCreateSerialzier, UUUUUU
from apps.users.models import User
from utils.serch import UserFilter


class UserListCreateView(ListCreateAPIView):
    serializer_class =  UserCreateSerialzier
    queryset = User.objects.all().order_by('id')
    # filterset_class = UserFilter
    # filter_backends = (SearchFilter)
    # filter_backends = []
    def get_queryset(self):
        # 获取前端传递的keyword值
        keyword = self.request.query_params.get('keyword')
        # 如果keyword是空字符，则说明要获取所有用户数据
        if keyword:
            return User.objects.filter(username__contains=keyword)
        else:
            return User.objects.all()
        # if keyword is '' or keyword is None:
        #     return User.objects.all()
        # else:
        #     return User.objects.filter(username=keyword)





class UserView(ListCreateAPIView):
    queryset = User.objects.all()
    serializer_class =  UUUUUU
    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return User.objects.filter(username__contains=keyword).order_by('id')
        else:
            return User.objects.all().order_by('id')


class AAA(ListCreateAPIView):
    queryset = User.objects.all().order_by('id')
    serializer_class = UUUUUU
    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return User.objects.filter(username__contains=keyword).order_by('id')
        else:
            return User.objects.all().order_by('id')



















